﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.Entity;
using WHC.Hotel.BLL;
using WHC.OrderWater.Commons;


namespace WHC.Hotel.UI
{
    public partial class FrmEditKTVGoodsType : BaseForm
    {
        public string ID = string.Empty;

        public FrmEditKTVGoodsType()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmEditRoomType_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ID))
            {
                this.Text = "编辑 " + this.Text;
                KTVGoodsTypeInfo info = BLLFactory<KTVGoodsType>.Instance.FindByID(ID);
                if (info != null)
                {
                    this.txtGoodsType.Text = info.GoodsType;
                    this.txtNote.Text = info.Note;
                    this.txtTypeNo.Text = info.TypeNo;
                    this.chkNeedService.Checked = info.IsNeedService;
                }
                //this.btnOK.Enabled = Portal.gc.HasFunction("Product/Modify");
            }
            else
            {
                this.Text = "新建 " + this.Text;
                //this.btnOK.Enabled = Portal.gc.HasFunction("Product/Add");
            }
            this.txtTypeNo.Focus();
        }

        private void SetInfo(KTVGoodsTypeInfo info)
        {
            info.GoodsType = this.txtGoodsType.Text.Trim();
            info.IsNeedService = this.chkNeedService.Checked;
            info.Note = this.txtNote.Text;
            info.TypeNo = this.txtTypeNo.Text.Trim();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {  
            if (this.txtTypeNo.Text.Trim().Length == 0)
            {
                MessageUtil.ShowTips("请输入类别编号");
                this.txtTypeNo.Focus();
                return;
            }
            if (this.txtGoodsType.Text.Trim().Length == 0)
            {
                MessageUtil.ShowTips("请输入商品类别");
                this.txtGoodsType.Focus();
                return;
            }

            if (!string.IsNullOrEmpty(ID))
            {
                KTVGoodsTypeInfo info = BLLFactory<KTVGoodsType>.Instance.FindByID(ID);
                if (info != null)
                {
                    SetInfo(info);

                    try
                    {
                        bool succeed = BLLFactory<KTVGoodsType>.Instance.Update(info, info.ID.ToString());
                        if (succeed)
                        {
                            MessageUtil.ShowTips("保存成功");
                            this.DialogResult = DialogResult.OK;
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error(ex);
                        MessageUtil.ShowError(ex.Message);
                    }
                }
            }
            else
            {
                KTVGoodsTypeInfo info = new KTVGoodsTypeInfo();
                SetInfo(info);

                try
                {
                    bool succeed = BLLFactory<KTVGoodsType>.Instance.Insert(info);
                    if (succeed)
                    {
                        MessageUtil.ShowTips("保存成功");
                        this.DialogResult = DialogResult.OK;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    MessageUtil.ShowError(ex.Message);
                }
            }
        }
    }
}
